<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //call(object,num1,num2,......)
        //apply(object,[num1,num2,....])
        //call和apply传参列表不同
        //call apply 改变this的指向
        //谁调用call this就指向谁
        function Person (name,age){
            this.name =name;
            this.age = age
        }
        var person = new Person('黄越',21)
        var obj = {}
        Person.call(obj,"韩",22)//隐式在构造函数里生成 this = obj
        //通过构造函数来构造其他对象
        //通过Person来构造obj

        //通过call来构造
        function Student (name,age,sex,tel){
           Person.call(this,name,age);//改变person构造器的this为student构造器的this
           this.sex =sex;
           this.tel = tel;
        }
        var student = new Student('帅比',100,'male',154645666666465)


        /**
        手写call、apply
        **/ 

        Function.prototype.myCall= function(context){
            /**判断是否传入对象
            传入对象这令this指向obj
            没传入对象就令this指向全局window
            **/
            var context = context || window;
            //将要执行的函数变成对象的属性
            context.fn = this;//obj.fn = Function
            //arguments 是类数组，此时arguments[0] = context
            //先封装成数组再去除第一位
            var args = [...arguments].slice(1)
            let reslut = context.fn(...args)//运用es6操作扩展符,将所有实参传入函数Function
            delete context.fn
            return reslut
        }
        
        Function.prototype.myApply = function(context){
            var context2 = context2 || window;
            context2.fn = this;
            let result;
            if(arguments[1]){
                result = context2.fn(...arguments[1])
            }else{
                result = context2.fn();
            }
            delete context2.fn;
            return result
        }
        function f(a,b,c){
            console.log(a,b,c)
        }
        f.myCall(null,1,2,3)
        console.log(Math.max.apply(null,[1,2,3,4,5,6]))
        console.log(Math.max.myApply(null,[1,2,3,4,5,6]))


    </script>
</body>
</html>